﻿package  src.game.managers {
	import src.Const;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.display.MovieClip;
	import src.game.Letter;
	
	public class ObjectFactory extends EventDispatcher {
		private var objectsToDisplay:Vector.<Letter>;
		private var objectsToBeCreated:Vector.<int>;
		//the timer that calls placing new object
		private var timer:Timer;
		private var timerDelay:int;
		
		private var objectProperties:ObjectProperties;
		
		public function ObjectFactory(timerDelay:int) {
			objectsToDisplay = new Vector.<Letter>();
			objectsToBeCreated = new Vector.<int>();
			this.timerDelay = timerDelay;
		}
		
		public function nextLevel(objectsList:Vector.<int>):void {
			trace("delay " + timerDelay);
			objectsToBeCreated = objectsList;
			initTimer(objectsList.length);
		}
		
		
		private function initTimer(totalObjects:int):void {
			if (timer == null)
				timer = new Timer(0, totalObjects);
			timer.delay = timerDelay;
			timer.addEventListener(TimerEvent.TIMER, placeNewObject);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, noNewObjects);
		}
		
		private function placeNewObject(event:TimerEvent):void {
			var objectIndex:int = Math.floor( Math.random() * objectsToBeCreated.length );
			var letter:Letter = new Letter(objectsToBeCreated[objectIndex]);
			objectsToBeCreated.splice(objectIndex, 1);
			letter = objectProperties.setProperties(letter);
			objectsToDisplay.push(letter);
			dispatchEvent(new Event(Event.ADDED));;
		}
		
		
		private function noNewObjects(event:TimerEvent):void {
			timer.removeEventListener(TimerEvent.TIMER, placeNewObject);
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE, noNewObjects);
			timer.stop();
			timer = null;
			dispatchEvent(new Event(Event.CLEAR));
		}
		
		public function startPlacingObjects():void {
			if (timer != null)
				timer.start();
		}
		
		
		public function stopPlacingObjects():void {
			if (timer != null)
				timer.stop();;
		}
		
		public function setObjectProperties(properties:ObjectProperties):void {
			this.objectProperties = properties;
		}
		
		public function clearAllObjectsToDisplay():void {
			objectsToDisplay.splice(0, objectsToDisplay.length);
		}
		
		public function numberOfObjectsToDisplay():int {
			return objectsToDisplay.length;
		}
		
		public function numberOfObjectsToBeCreated():int {
			return objectsToBeCreated.length;
		}
		
		public function getObjectAt(index:int):Letter {
			return objectsToDisplay[index];
		}
		
		public function setObjectsSpeed(speed:int):void {
			objectProperties.setSpeed(speed);
		}
		
		public function setObjectsRotationSpeed(speed:int):void {
			objectProperties.setRotation(speed);
		}
		
		public function removeAll():void {
			objectsToDisplay.splice(0, objectsToDisplay.length);
			objectsToBeCreated.splice(0, objectsToBeCreated.length);
		}
	}
}